package com.jeesite.modules.sd.common.util;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sd.common.constants.DocLogEnum;
import com.jeesite.modules.sd.entity.DocLog;
import com.jeesite.modules.sd.entity.DocRecv;
import com.jeesite.modules.sd.service.DocLogService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 文件操作日志工具类
 *
 * @author 雍帅杰
 */
@Service
public class LogUtil {

    @Autowired
    private DocLogService docLogService;

    private static LogUtil logUtil;


    @PostConstruct
    public void init() {
        logUtil = this;
        logUtil.docLogService = this.docLogService;
    }

    /**
     * 日志工具类
     *
     * @param docId      文件Id
     * @param docTitle   来文标题
     * @param docFullNum 来文文号
     * @param logContent 日志内容
     * @param docSecLvl  来文密级
     * @param docLogEnum 日志类型
     */
    public static void LogInsert(String docId, String docTitle, String docFullNum, String logContent, String docSecLvl, DocLogEnum docLogEnum) {
        DocLog DocLog = new DocLog();
        DocLog.setDocId(docId);
        DocLog.setDocTitle(docTitle);
        DocLog.setDocFullNum(docFullNum);
        DocLog.setLogContent(logContent);
        DocLog.setSecLvl(docSecLvl);
        DocLog.setLogType(docLogEnum.getKey());
        DocLog.setLogDate(new Date());
        DocLog.setOptUserCode(UserUtils.getUser().getUserCode());
        DocLog.setOptUserName(UserUtils.getUser().getUserName());

        if (StringUtils.isNotBlank(UserUtils.getUser().getExtend().getExtendS1())) {
            DocLog.setOptUserPoliceno(UserUtils.getUser().getExtend().getExtendS1());
        }
        logUtil.docLogService.saveLog(DocLog);
    }

    /**
     * 记录文件操作日志
     *
     * @param docRecv    文件信息(被操作的文件)
     * @param logContent 日志内容
     * @param docLogEnum 日志类型
     */
    public static void insertLog(DocRecv docRecv, String logContent, DocLogEnum docLogEnum) {
        DocLog docLog = initLog(docRecv, logContent, docLogEnum);
        logUtil.docLogService.saveLog(docLog);
    }

    /**
     * 批量记录文件操作日志
     *
     * @param docLogs 可直接保存的日志list
     */
    public static void batchInsertLog(List<DocLog> docLogs) {
        if (CollectionUtils.isNotEmpty(docLogs)) {
            for (DocLog log : docLogs) {
                logUtil.docLogService.save(log);
            }
        }
    }

    /**
     * 初始化log文件信息
     *
     * @param docRecv    文件信息(被操作的文件)
     * @param logContent 日志内容
     * @param docLogEnum 日志类型
     */
    public static DocLog initLog(DocRecv docRecv, String logContent, DocLogEnum docLogEnum) {
        DocLog docLog = new DocLog();
        docLog.setDocId(docRecv.getId());
        docLog.setDocTitle(docRecv.getDocTitle());
        docLog.setDocFullNum(docRecv.getDocFullNum());
        docLog.setLogContent(logContent);
        docLog.setSecLvl(docRecv.getSecLvl());
        docLog.setLogType(docLogEnum.getKey());
        docLog.setLogDate(new Date());
        //获取当前登录人信息
        User optUser = UserUtils.getUser();
        if (Objects.nonNull(optUser) && null != optUser.getId()) {
            docLog.setOptUserCode(optUser.getUserCode());
            docLog.setOptUserName(optUser.getUserName());
            if (StringUtils.isNotEmpty(optUser.getExtend().getExtendS1())) {
                docLog.setOptUserPoliceno(optUser.getExtend().getExtendS1());
            }
        }
        return docLog;
    }
}
